/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * PlaylistCellWithArtPanel.java
 *
 * Created on Mar 8, 2009, 2:36:51 PM
 */
package com.bff.slimserver.musicjukebox.ui.components;

import com.bff.bjj.ui.component.BJJPanel;
import com.bff.slimserver.music.exceptions.PlaylistException;
import com.bff.bjj.ui.component.ColorProperties;
import com.bff.slimserver.musicjukebox.lib.JukeboxProperties;
import com.bff.slimserver.musicjukebox.ui.ThumbnailPanel;
import com.bff.slimserver.musicjukebox.ui.components.favorites.FavoritesAddButton;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.bff.slimserver.SlimPlaylist;
import org.bff.slimserver.exception.SlimConnectionException;
import org.bff.slimserver.exception.SlimDatabaseException;
import org.bff.slimserver.musicobjects.SlimAlbum;
import org.bff.slimserver.musicobjects.SlimArtist;

/**
 *
 * @author Music
 */
public class AlbumCellWithArtPanel extends javax.swing.JPanel implements AlbumCellPanelIface {

    private boolean selected;
    private SlimPlaylist playlist;
    private ThumbnailPanel tp;
    private SlimAlbum album;

    /**
     * Creates new form PlaylistCellWithArtPanel
     * Delay is the number of milliseconds to wait before loading the thumbnail
     */
    public AlbumCellWithArtPanel(final SlimAlbum album, final int delay) {
        this.album = album;
        try {
            this.playlist = JukeboxProperties.getInstance().getPlaylist();
        } catch (PlaylistException ex) {
            Logger.getLogger(AlbumCellWithArtPanel.class.getName()).log(Level.SEVERE, null, ex);
        }

        initComponents();

        lblAlbum.setText(album.getName());

        if (album.getArtist() == null) {
            try {
                album.setArtist(new ArrayList<SlimArtist>(JukeboxProperties.getInstance().getDatabase().getArtistsForAlbum(album)).get(0).getName());
            } catch (SlimConnectionException ex) {
                Logger.getLogger(AlbumCellWithArtPanel.class.getName()).log(Level.SEVERE, null, ex);
            } catch (SlimDatabaseException ex) {
                Logger.getLogger(AlbumCellWithArtPanel.class.getName()).log(Level.SEVERE, null, ex);
            }
        }

        if (album.getArtist() != null) {
            lblArtist.setText(album.getArtist());
        } else {
            detailPanel.remove(lblArtist);
        }

        if (album.getYear() != null && !"".equalsIgnoreCase(album.getYear())) {
            lblYear.setText(album.getYear());
        } else {
            songPanel.remove(lblYear);
        }

        tp = new ThumbnailPanel();
        tp.setPreferredSize(new Dimension(100, 100));
        artPanel.add(tp, BorderLayout.CENTER);

        repaint();

        new Thread(new Runnable() {

            @Override
            public void run() {
                try {
                    Thread.sleep(delay);
                } catch (InterruptedException ex) {
                    Logger.getLogger(AlbumCellWithArtPanel.class.getName()).log(Level.SEVERE, null, ex);
                }

                tp.loadImage(album, 0);
            }
        }).start();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        java.awt.GridBagConstraints gridBagConstraints;

        artPanel = new javax.swing.JPanel();
        detailPanel = new BJJPanel();
        songPanel = new javax.swing.JPanel();
        lblAlbum = new javax.swing.JLabel();
        lblYear = new javax.swing.JLabel();
        lblArtist = new javax.swing.JLabel();
        jPanel1 = new BJJPanel();
        btnAddFavorite = new FavoritesAddButton(getAlbum());
        btnAddFavorite.setText(null);
        btnAdd = new AlbumAddButton(getPlaylist(), getAlbum());
        btnAdd.setText(null);

        setLayout(new java.awt.BorderLayout());

        artPanel.setBackground(new java.awt.Color(0, 0, 0));
        artPanel.setBorder(javax.swing.BorderFactory.createEmptyBorder(5, 5, 5, 5));
        artPanel.setPreferredSize(new java.awt.Dimension(100, 100));
        artPanel.setLayout(new java.awt.BorderLayout());
        add(artPanel, java.awt.BorderLayout.WEST);

        detailPanel.setLayout(new java.awt.BorderLayout());

        songPanel.setBorder(javax.swing.BorderFactory.createEmptyBorder(10, 10, 10, 10));
        songPanel.setLayout(new java.awt.GridBagLayout());

        lblAlbum.setText("jLabel2");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        songPanel.add(lblAlbum, gridBagConstraints);

        lblYear.setText("jLabel2");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        songPanel.add(lblYear, gridBagConstraints);

        lblArtist.setText("jLabel3");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        songPanel.add(lblArtist, gridBagConstraints);

        detailPanel.add(songPanel, java.awt.BorderLayout.WEST);

        add(detailPanel, java.awt.BorderLayout.CENTER);

        jPanel1.setLayout(new java.awt.GridBagLayout());

        btnAddFavorite.setFont(new java.awt.Font("Tahoma", 0, 10));
        jPanel1.add(btnAddFavorite, new java.awt.GridBagConstraints());

        btnAdd.setFont(new java.awt.Font("Tahoma", 0, 10));
        jPanel1.add(btnAdd, new java.awt.GridBagConstraints());

        add(jPanel1, java.awt.BorderLayout.EAST);
    }// </editor-fold>//GEN-END:initComponents
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JPanel artPanel;
    private javax.swing.JButton btnAdd;
    private javax.swing.JButton btnAddFavorite;
    private javax.swing.JPanel detailPanel;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JLabel lblAlbum;
    private javax.swing.JLabel lblArtist;
    private javax.swing.JLabel lblYear;
    private javax.swing.JPanel songPanel;
    // End of variables declaration//GEN-END:variables

    @Override
    public void setEven() {
        setBackground(ColorProperties.LIST_BACKGROUND_EVEN);
    }

    @Override
    public void setOdd() {
        setBackground(ColorProperties.LIST_BACKGROUND_ODD);
    }

    @Override
    public void setCurrentPlaying() {
        setForeground(ColorProperties.PLAYLIST_CURRENT_PLAYING_FOREGROUND);
    }

    @Override
    public void setSelected() {
        setForeground(ColorProperties.LIST_FOREGROUND_SELECTED);
        setBackground(ColorProperties.LIST_BACKGROUND_SELECTED);
        showAddButton();
    }

    @Override
    public void setNotSelected() {
        setForeground(ColorProperties.LIST_FOREGROUND);
        hideAddButton();
    }

    @Override
    public void setBackground(Color bg) {
        super.setBackground(bg);
        if (jPanel1 != null) {
            jPanel1.setBackground(bg);
        }

        //detailPanel.setBackground(bg);
        if (detailPanel != null) {
            detailPanel.setBackground(bg);
            songPanel.setBackground(bg);
        }
    }

    @Override
    public void setForeground(Color fg) {
        super.setForeground(fg);
        if (jPanel1 != null) {
            jPanel1.setForeground(fg);
        }

        if (detailPanel != null) {
            detailPanel.setForeground(fg);
            songPanel.setForeground(fg);
        }
        setLabelColors(fg);
    }

    public void setAddButtonVisible(boolean visible) {
        jPanel1.setVisible(visible);
    }

    public void showAddButton() {
        setAddButtonVisible(true);
    }

    public void hideAddButton() {
        setAddButtonVisible(false);
    }

    private void setLabelColors(Color color) {
        if (lblArtist != null) {
            lblArtist.setForeground(color);
            lblAlbum.setForeground(color);
            lblYear.setForeground(color);
        }
    }

    @Override
    public Component getPanel() {
        return this;
    }

    /**
     * @return the selected
     */
    public boolean isSelected() {
        return selected;
    }

    public SlimPlaylist getPlaylist() {
        return playlist;
    }

    public void setPlaylist(SlimPlaylist playlist) {
        this.playlist = playlist;
    }

    @Override
    public void setCurrent() {
        setLabelColors(ColorProperties.PLAYLIST_CURRENT_PLAYING_FOREGROUND);
    }

    @Override
    public void setNotCurrent() {
        setLabelColors(ColorProperties.LIST_FOREGROUND_SELECTED);
    }

    @Override
    public void loadThumbnail() {
        tp.loadImage(getAlbum());
    }

    /**
     * @return the album
     */
    public SlimAlbum getAlbum() {
        return album;
    }

    /**
     * @param album the album to set
     */
    public void setAlbum(SlimAlbum album) {
        this.album = album;
    }

    @Override
    public void hideArtist() {
    }

    @Override
    public void showArtist() {
    }

    @Override
    public void showYear() {
    }

    @Override
    public void hideYear() {
    }
}
